<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 50px;
            top: 50px;

        }
    </style>
</head>

<body>
    <div class="shadow"></div>
</body>
<script>
    // 需求: 盒子跟随鼠标在文档中移动?
    // 1. 鼠标先移动 => 知道鼠标移动的位置
    // 2. 盒子跟随鼠标移动 => 怎么动? (定位 left top调整盒子的位置)
    // 3. 改中心跟随

    var shadow = document.querySelector(".shadow");

    document.onmousemove = function (e) {
        var e = e || window.event;

        var x = e.pageX - shadow.clientWidth / 2;
        var y = e.pageY - shadow.clientHeight / 2;
        console.log(x, y);

        // 取值时记得去单位,赋值时记得加单位
        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

    }



</script>

</html>